package com.hxalex.simulationhandle.accessbilityService.receiver.search;

import android.content.Intent;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.accessbilityService.HandleService;
import com.hxalex.simulationhandle.accessbilityService.manger.CommandManger;
import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.manger.WifiManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;
import com.hxalex.simulationhandle.accessbilityService.receiver.search.serivce.ClickWebContentService;

/**
 * Created by hx_Alex on 2018/5/31.
 */

public class SearchWebContentReceiver extends BaseReceiver {
    private AccessibilityNodeInfo nodeInfo;
    private boolean isStartClickWebContentService=false;
    private boolean isStartCheckSearchTimeService=false;
    private  SearchWebContentReceiver.toBackPreWebSiteListener toBackPreWebSiteListener;
   
    public AccessibilityNodeInfo getNodeInfo() {
        return nodeInfo;
    }

    public void setNodeInfo(AccessibilityNodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }

    public SearchWebContentReceiver(SearchManger searchManger) {
        super(searchManger);
        FLAG="检索网页内容并尝试点击点击命令";
    }

    public void setToBackPreWebSiteListener(SearchWebContentReceiver.toBackPreWebSiteListener toBackPreWebSiteListener) {
        this.toBackPreWebSiteListener = toBackPreWebSiteListener;
    }
    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("SearchWebContent","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        if(nodeInfo==null) return;
        if(searchManger.recycleSearchWebSiteInKeyWordReslut(nodeInfo))
        {
            ClickWebContentService.isExecuteLoop=true;
            Intent i=new Intent(SearchManger.getContext(), ClickWebContentService.class);
            SearchManger.getContext().startService(i);
            commandCompleteListener.commandCompleteListener();
          
        }else
        {
            //当当前关键词的所有匹配项执行完成后，执行下一个关键词的检索
            if(searchManger.isNotMatch)
            {
                searchManger.isNotMatch=false;
                //如果列表数据为空，则当前关键词的网页全部检索完毕
                //开始执行下一个关键词的检索
                if(searchManger.removeNowKeyWord())
                {
                    Log.i("SearchWebContent","开始执行下一个关键词的检索");
                   searchManger.openBrowser();
                    SearchManger.getNewSearchManger(SearchManger.getContext(),searchManger.getKeyWordData(),
                            searchManger.getWebSiteData(),
                            searchManger.getLoadKeyWordData(),
                            searchManger.getLoadWebSiteData(),
                            searchManger.getWebSiteStopTime(),
                            searchManger.getTaskLoopNum(),searchManger.getNowTaskLoopNum());
                    CommandManger.getManger().loadBrowserCommand(SearchManger.getContext(),true);
                }else
                {
                    //当前循环次数的所有关键词检索完毕
                    if(searchManger.getKeyWordData().size()==0)
                    {
                        Log.i("SearchWebContent","当前循环次数的所有关键词检索完毕");
                        //判断循环次数
                        int nowTaskLoopNum = searchManger.getNowTaskLoopNum()+1;
                        int taskLoopNum = searchManger.getTaskLoopNum();
                        searchManger.setNowTaskLoopNum(nowTaskLoopNum);
                        if(nowTaskLoopNum>taskLoopNum)
                        {
                            //本次任务执行完毕
                            Log.i("SearchWebContent","本次任务执行完毕");
                            HandleService.isStartService=false;
                            //返回当前应用
                            Intent i = SearchManger.getContext().getPackageManager().getLaunchIntentForPackage(SearchManger.getContext().getResources().getString(R.string.main_app_package_name));
                            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            SearchManger.getContext().startActivity(i);
                        }else 
                        {
                            Log.i("SearchWebContent","当前循环次数的所有关键词检索完毕,开始执行下一循环 当前循环次数:"+nowTaskLoopNum+"  总计循环次数:"+taskLoopNum);
                            SearchManger newSearchManger = SearchManger.getNewSearchManger(SearchManger.getContext(), searchManger.getKeyWordData(),
                                    searchManger.getWebSiteData(),
                                    searchManger.getLoadKeyWordData(),
                                    searchManger.getLoadWebSiteData(),
                                    searchManger.getWebSiteStopTime(),
                                    searchManger.getTaskLoopNum(), searchManger.getNowTaskLoopNum());
                            newSearchManger.setKeyWordData(searchManger.getLoadKeyWordData());
                            newSearchManger.setWebSiteData(searchManger.getLoadWebSiteData());
                            WifiManger wifiManger = WifiManger.getWifiManger(SearchManger.getContext());
                            wifiManger.openWifiHelper();
                            wifiManger.loadAllWifiCommand();
                        }

                    }
                }
                commandCompleteListener.commandCompleteListener();
            }
   
        }

    }

    public interface toBackPreWebSiteListener
    {
        void toBackPreWebSiteToSearchWebContent();
    }


    
}
